var SAVE_BUTTON_ID = 'saveButtonId';
var ERRORS_LIST_ID = 'errorsListId';

$(document).ready(function(){
	$('.form-button-logout').css('display', 'block');
	$('.form-textarea').attr('maxlength', 100);
    
    $('.form-textarea').bind('keyup blur',function(event){validateFields();});
	$('.editeating-form-field-label').each(function(i, f){
		fieldLabelsMap[$('#'+f.id).attr('for')]=$('#'+f.id).text();
	});
	
	$('#'+SAVE_BUTTON_ID).click(function(event){
		validateBeforeSubmit();				
	});
	
	$('#clearButtonId').click(function(event){
		clearFields();				
	});
	
	$('#timeFieldId').attr('readOnly', true);
	
	$('#timeFieldId').timepicker({
	    // Options
	    timeSeparator: ':',           
	    periodSeparator: ' ',       
	    defaultTime: '12:34',         
	    showOn: 'focus',           
	
	    // Localization
	    hourText: 'Часы',             
	    minuteText: 'Минуты',         
	    amPmText: ['AM', 'PM'],       
	    // Position
	    myPosition: 'left top',       
	    atPosition: 'left bottom',    
	
	    // custom hours and minutes
	    hours: {
	        starts: 0,                
	        ends: 23                  
	    },
	    minutes: {
	        starts: 0,                
	        ends: 55,                 
	        interval: 5               
	    },
	    rows: 4,                      
	    showHours: true,              
	    showMinutes: true,            
	
	    // buttons
	    showCloseButton: false,       
	    closeButtonText: 'Закрыть',      
	    showNowButton: true,         
	    nowButtonText: 'Сейчас'
	});
});

//validates all form fields before submitting form
function validateBeforeSubmit(){
	if(validateFields()){
		$('#editEatingFormId').submit();
	}
};

//resets from
function clearFields(){
	validateFields();
	$('#editEatingFormId').reset();
	$('.form-textarea').removeClass('form-field-valid');
	$('.form-textarea').removeClass('form-field-invalid');
	$('.form-textarea').val('');
};

function validateFields(){
	var validTotal = true;
	var validField = null;
	var errorCount = 0;
	$('#'+ERRORS_LIST_ID).empty();
		
	validField = true;
	if(!isValidText($.trim($('.form-textarea').val()))){
		validField = false;
		errorCount++;
		$('#'+ERRORS_LIST_ID).append('<li>'+messages['message.error.field']+' <strong>'+fieldLabelsMap[$('.form-textarea')[0].id]+'</strong> '+messages['message.error.field.contain.incorrect.symbols']+'</li>');
	}	
	if(!validField){
		validTotal=false;
		$('.form-textarea').removeClass('form-field-valid');
		$('.form-textarea').addClass('form-field-invalid');
	}else{
		$('.form-textarea').removeClass('form-field-invalid');
		$('.form-textarea').addClass('form-field-valid');
	}
	
	if(!validTotal){
		var h = 100+20*errorCount;
		$('.registration-form-error-wrapper').css('height', h+'px');
		$('.registration-form-error-wrapper').css('display', 'inline-block');
		disableButton(SAVE_BUTTON_ID);
		return false;
	}else{
		$('.registration-form-error-wrapper').css('display', 'none');
		enableButton(SAVE_BUTTON_ID);
		return true;
	}
}

